package com.demo;

import com.demo.controller.interceptor.UserInfoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 注册拦截器要继承 WebMvcConfigurer 接口, 并重写 addInterceptors 方法
 * 添加具体的拦截器, 并指定具体 api
 * */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

//    在 spring boot 容器中放入拦截器
    @Bean
    public UserInfoInterceptor userInfoInterceptor() {
        return new UserInfoInterceptor();
    }

//    注册拦截器, 并且拦截指定路由
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInfoInterceptor()).addPathPatterns("/getStudent");
    }
}
